package practice;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    Queue<Integer> qu1;
    Queue<Integer> qu2;
    int usedsize;
//构造方法
    public MyStack() {
    qu1 = new LinkedList<>();
    qu2 = new LinkedList<>();
    }
//入栈:入不为空的队列
    //初始入栈:默认qu1
    public void push(int x) {
        if(!qu1.isEmpty()){
            qu1.offer(x);
        }else if(!qu2.isEmpty()){
            qu2.offer(x);
        }else {
            qu1.offer(x);
        }
        usedsize++;

    }
//出栈:出不为空队列size-1
    public int pop() {
        //空栈
        if(empty()){
            return -1;
        }else if(!qu1.isEmpty()){
            int cursize = qu1.size();
            for(int i = 0; i < cursize-1; i++){
                qu2.offer(qu1.poll());
            }
            usedsize--;
            return qu1.poll();
        }else {
            int cursize = qu2.size();
            for(int i = 0; i < cursize-1; i++){
                qu1.offer(qu2.poll());
            }
            usedsize--;
            return qu2.poll();
        }

    }

    public int top() {
        //空栈
        if(empty()){
            return -1;
        }else if(!qu1.isEmpty()){
            int cursize = qu1.size();
            int tmp = -1;
            for(int i = 0; i < cursize; i++){
                tmp = qu1.poll();
                qu2.offer(tmp);
            }

            return tmp;
        }else {
            int cursize = qu2.size();
            int tmp = -1;
            for(int i = 0; i < cursize; i++){
                tmp = qu2.poll();
                qu1.offer(tmp);
            }

            return tmp;
        }

    }

    public boolean empty() {
        return  usedsize == 0;
    }
}
